<template>
    <real-el-link @click.prevent="btnClick">
        <slot></slot>
    </real-el-link>
</template>

<script setup lang="ts">
import { ElLink as RealElLink } from 'element-plus'
import { useAttrs } from 'vue'
import { ElMessageBox } from 'element-plus'
defineOptions({ name: 'ElLink' })
const props = defineProps(['auth', 'prevent', 'openType', 'name'])
const emit = defineEmits(['async'])

const attrs: any = useAttrs()
const win: any = window
let preventClick = false

const btnClick = () => {
    if (attrs.click) {
        attrs.onClick()
        if (props.openType) {
            win[props.openType] && win[props.openType]()
        }
    }
    if (!preventClick) {
        preventClick = true
        emit('async', () => {
            setTimeout(() => {
                preventClick = false;
                if (props.openType) {
                    win[props.openType] && win[props.openType]()
                }
            }, 200)
        })
    }

    if (attrs.onConfirm) {
        if (props.prevent === false) {
            attrs.onConfirm()
            if (props.openType) {
                win[props.openType] && win[props.openType]()
            }
        } else {
            ElMessageBox.confirm(
                '当前内容可能存在修改，确认继续?',
                '提示',
                {
                    confirmButtonText: '确认',
                    cancelButtonText: '取消',
                    type: 'warning',
                }
            ).then(() => {
                attrs.onConfirm()
                if (props.openType) {
                    win[props.openType] && win[props.openType]()
                }
            }).catch(() => {
            })
        }
    } else {
        if (props.openType) {
            win[props.openType] && win[props.openType]()
        }
    }
}
</script>